package cn.iocoder.yudao.module.product.spu.api;

import cn.iocoder.yudao.framework.business.basic.pojo.dto.LocationQueryDTO;
import cn.iocoder.yudao.framework.business.basic.pojo.dto.SortQueryDTO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.product.spu.ProductSpuApi;
import cn.iocoder.yudao.module.api.product.spu.dto.ProductSpuDetailRespDTO;
import cn.iocoder.yudao.module.api.product.spu.dto.ProductSpuForUniAppRespDTO;
import cn.iocoder.yudao.module.api.product.spu.dto.ProductSpuPageReqDTO;
import cn.iocoder.yudao.module.api.product.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.product.spu.convert.ProductSpuConvert;
import cn.iocoder.yudao.module.product.spu.dal.dataobject.ProductSpuDO;
import cn.iocoder.yudao.module.product.spu.service.ProductSpuService;
import cn.iocoder.yudao.module.product.spu.vo.AppProductSpuDetailRespVO;
import cn.iocoder.yudao.module.product.spu.vo.AppProductSpuRespVO;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.api.product.ErrorCodeConstants.SPU_NOT_EXISTS;

/**
 * 产品 SPU API 接口实现类
 *
 * @author LeeYan9
 * @since 2022-09-06
 */
@Service
@Validated
public class ProductSpuApiImpl implements ProductSpuApi {

    @Resource
    @Lazy // 延迟加载，避免循环依赖
    private ProductSpuService productSpuService;

    @Override
    public List<ProductSpuRespDTO> getSpuList(Collection<Long> ids) {
        List<ProductSpuDO> spus = productSpuService.getSpuList(ids);
        return ProductSpuConvert.INSTANCE.convertDTOList(spus);
    }

    @Override
    public List<ProductSpuRespDTO> validateSpuList(Collection<Long> ids) {
        List<ProductSpuDO> spus = productSpuService.validateSpuList(ids);
        return ProductSpuConvert.INSTANCE.convertDTOList(spus);
    }

    @Override
    public ProductSpuDetailRespDTO getSpuDetail(Long id) {
        ProductSpuDO spu = productSpuService.getSpu(id);
        return ProductSpuConvert.INSTANCE.convert2(spu);
    }

    @Override
    public ProductSpuRespDTO validateSpu(Long id) {
        ProductSpuRespDTO spu = getSpu(id);
        if (spu == null) {
            throw exception(SPU_NOT_EXISTS);
        }
        return spu;
    }

    @Override
    public ProductSpuForUniAppRespDTO browseSpu(Long userId, Long id) {
        // 获得产品 SPU
        AppProductSpuDetailRespVO respVO = productSpuService.browseSpu(userId, id);
        // 转换返回
        return ProductSpuConvert.INSTANCE.convert2(respVO);
    }

    @Override
    public PageResult<ProductSpuRespDTO> getSpuPage(ProductSpuPageReqDTO dto) {
        PageResult<AppProductSpuRespVO> pageResult = productSpuService.getSpuPage(ProductSpuConvert.INSTANCE.convert(dto));
        return ProductSpuConvert.INSTANCE.convertPage(pageResult);
    }

    @Override
    public Long getProductCountByShopId(Long shopId) {
        return productSpuService.getProductCountByShopId(shopId);
    }

    @Override
    public Map<Long, Integer> getSpuCountMapByShopIds(Collection<Long> shopIds) {
        return productSpuService.getSpuCountMapByShopIds(shopIds);
    }

    @Override
    public Map<Long, Integer> getShopDistanceMapByPlatformCategoryIds(Set<Long> pcIds, LocationQueryDTO location, SortQueryDTO sort) {
        return productSpuService.getShopDistanceMapByPlatformCategoryIds(pcIds, location, sort);
    }

}
